package com.hyyt.shopjsp.common.utils;

import com.hyyt.shopjsp.basic.pojo.BasicActor;
import com.hyyt.shopjsp.basic.pojo.BasicActorPurview;
import com.hyyt.shopjsp.basic.pojo.BasicPurview;
import com.hyyt.shopjsp.util.common.LoggerUtils;
import com.hyyt.shopjsp.util.common.StringStaticKey;
import com.hyyt.shopjsp.util.common.Utils;

import java.util.*;

/**
 * Copyright (c) 2016 ShopJsp. All Rights Reserved.
 * ============================================================================
 * 版权所有 2011 - 今 北京华宇盈通科技有限公司，并保留所有权利。
 * ----------------------------------------------------------------------------
 * 提示：在未取得SHOPJSP商业授权之前，您不能将本软件应用于商业用途，否则SHOPJSP将保留追究的权力。
 * ----------------------------------------------------------------------------
 * 官方网站：http://www.shopjsp.com
 * ============================================================================
 * @author Cuiys on 2016/10/24 0024上午 9:47.
 */
public class PurviewUtil {
    /**
     * 根据权限id，获得集合中对应权限
     * @param basicPurviews
     * @param purviewId
     * @return
     */
    public static BasicPurview getBasicPurviewById(List<BasicPurview> basicPurviews, Integer purviewId) {
        if (Utils.objectIsNotEmpty(basicPurviews) && basicPurviews.size() > 0) {
            for (BasicPurview basicPurview : basicPurviews) {
                if (basicPurview.getPurviewId().compareTo(purviewId) == 0) {
                    return basicPurview;
                }
            }
        }
        return null;
    }

    /**
     * 获得集合中叶子根节点中所有权限
     * @param basicPurviews
     * @return
     */
    public static List<BasicPurview> getBasicPurviewByLeaf(List<BasicPurview> basicPurviews) {
        List<BasicPurview> purviews = new ArrayList<>();
        if (Utils.objectIsNotEmpty(basicPurviews) && basicPurviews.size() > 0) {
            for (BasicPurview basicPurview : basicPurviews) {
                if (basicPurview.getIsLeaf() == 0 && basicPurview.getParentId() == 0) {
                    purviews.add(basicPurview);
                }
            }
        }
        return purviews;
    }

    /**
     * 根据权限id获得下级权限集合
     * @param basicPurviews
     * @param purviewId 当前级别权限id
     * @return
     */
    public static List<BasicPurview> getNextPurviewByPurviewId(List<BasicPurview> basicPurviews, Integer purviewId) {
        if (Utils.objectIsEmpty(purviewId)) {
            return null;
        }
        List<BasicPurview> nextPurviews = new ArrayList<>();
        if (Utils.objectIsNotEmpty(basicPurviews) && basicPurviews.size() > 0) {
            for (BasicPurview purview : basicPurviews) {
                //与权限id比较，获得下级权限组
                if (purview.getParentId().compareTo(purviewId) == 0) {
                    nextPurviews.add(purview);
                }
            }
        }
        return nextPurviews;
    }

    /**
     * 根据第1级权限id获得其下级权限
     * @param basicPurviews 所有权限集合
     * @param level1PurviewId   第1组权限id
     * @return
     */
    public static List<BasicPurview> getBasicPurviewByLeaf(List<BasicPurview> basicPurviews, Integer level1PurviewId) {
        if (Utils.objectIsEmpty(level1PurviewId)) {
            return null;
        }
        List<BasicPurview> level2Purviews = new ArrayList<>();
        if (Utils.objectIsNotEmpty(basicPurviews) && basicPurviews.size() > 0) {
            for (BasicPurview purviews_2 : basicPurviews) {
                //与1级权限id比较，获得2级权限组
                if (purviews_2.getParentId().compareTo(level1PurviewId) == 0) {
                    int level2PurviewId = purviews_2.getPurviewId(); //声明并获得当前第2级权限Id
                    List<BasicPurview> level3Purviews = new ArrayList<>();  //声明3级权限组
                    for (BasicPurview purviews_3 : basicPurviews) {
                        //与2级权限id比较，获得3级权限组
                        if (purviews_3.getParentId().compareTo(level2PurviewId) == 0) {
                            level3Purviews.add(purviews_3); //存储3级权限
                        }
                    }
                    purviews_2.setBasicPurviews(level3Purviews);    //把3级权限组放入2级权限对象
                    level2Purviews.add(purviews_2); //存储2级权限
                }
            }
        }

        return level2Purviews;
    }

    /**
     * 获得当前角色下所有权限链接URL
     *
     * @return 权限URL集合 List<String>
     */
    public static Set<String> getPermissionsUrlSet(List<BasicPurview> basicPurviews) {
        Set<String> setPurviews = new HashSet<String>();
        if (Utils.objectIsNotEmpty(basicPurviews) && basicPurviews.size() > 0) {
            for (BasicPurview purview : basicPurviews) {
                //purview.getUrl() 用户权限URL不为空并且以 back/ 开头的权限才能保存到shiro
                if(Utils.objectIsNotEmpty(purview.getUrl()) && purview.getUrl().startsWith(StringStaticKey.PURVIEW_USER_PREFIX)) {
                    setPurviews.add(purview.getUrl());
                }
            }
        }
        return setPurviews;
    }

    /**
     * 获得当前角色下所有菜单权限链接URL及菜单权限对应的功能权限集合
     *
     * @return 权限URL集合 List<String>
     */
    public static Set<String> getPermissionsFunctionSet(List<BasicPurview> basicPurviews) {
        Set<String> setPurviews = new HashSet<>();
        if (Utils.objectIsNotEmpty(basicPurviews) && basicPurviews.size() > 0) {
            for (BasicPurview purview : basicPurviews) {
                //菜单权限验证编码
                String purviewValidateCode = purview.getFunctions();
                //purviewUrl 用户权限URL
                String purviewUrl = purview.getUrl();
                //purviewUrl 用户权限URL不为空并且以 back/ 开头的权限才能保存到shiro
                if(Utils.objectIsNotEmpty(purviewUrl) && purviewUrl.startsWith(StringStaticKey.PURVIEW_STARTSWITH_PREFIX)) {
                    //菜单权限链接URL添加
                    setPurviews.add(purviewUrl);

                    purviewValidateCode = purviewUrl.substring(purviewUrl.lastIndexOf("/"),purviewUrl.length()).replaceFirst("/", "");
                    purviewValidateCode = purviewValidateCode.substring(0,purviewValidateCode.lastIndexOf("."));

                    //functions 用户菜单权限对应功能按钮权限组，例如：267_添加_add,267_修改_update,267_删除_delete
                    String functions = purview.getFunctions();
                    if (Utils.objectIsNotEmpty(functions)) {
                        String [] functionsArray=functions.split(",");
                        if(functionsArray!=null && functionsArray.length>0){
                            for(String function:functionsArray){
                                String functionInfos = function.substring(function.lastIndexOf("_"),function.length());
                                /**
                                 * purviewFunction 用户菜单权限对应功能按钮权限，functionInfos 为 _add , _update , _delete
                                 * purviewFunction 组合存储 shiro 中，例如：back_gotoKeyBookPage_add
                                 */
                                String purviewFunction = StringStaticKey.PURVIEW_FUNCTION_PREFIX + purviewValidateCode + functionInfos;
                                //用户菜单权限对应功能按钮权限添加
                                setPurviews.add(purviewFunction);
                            }
                        }
                    }
                }
            }
        }
        return setPurviews;
    }

    /**
     * 用户的角色ID集合
     *
     * @return
     */
    public static Set<String> getActorIdSet(List<BasicActor> actors){
        Set<String> set=new HashSet<String>();
        if (Utils.objectIsNotEmpty(actors) && actors.size() > 0) {
            for (BasicActor actor : actors) {
                set.add(String.valueOf(actor.getActorId()));
            }
        }
        return set;
    }

    /**
     * 删除 ArrayList 中重复权限元素，保持顺序
     *
     * @return
     */
    public static void removeDuplicateWithOrder(List<BasicPurview> list) {
        Set set = new HashSet();
        List newList = new ArrayList();
        for (Iterator iter = list.iterator(); iter.hasNext();) {
            BasicPurview basicPurview = (BasicPurview) iter.next();
            if (set.add(basicPurview.getPurviewId())) {
                newList.add(basicPurview);
            }
        }
        list.clear();
        list.addAll(newList);
    }

}